package com.oraycn.esbasic.helpers;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

public class SystemHelper {

    /**
     * 打开系统浏览器跳转到指定的地址
     * @param context
     * @param url 要跳转的地址
     */
    public static void openBrowser(Context context,String url)
    {
        if (url.contains("http")) {
            //通过浏览器打开URL
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));    //splitflowurl为分流地址
            if (!SystemHelper.hasPreferredApplication(context,intent)){
                intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
            }
            context.startActivity(intent);
        }
    }

    //判断系统是否设置了默认浏览器
    public static boolean hasPreferredApplication(Context context, Intent intent) {
        PackageManager pm = context.getPackageManager();
        ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return !"android".equals(info.activityInfo.packageName); //如果info.activityInfo.packageName为android,则没有设置,否则,有默认的程序.
    }

}
